#ifndef _FILELOADERIMP_H_
#define _FILELOADERIMP_H_

/** This file assumes that the following macros are defined:

		SEPARATOR :  a character specifying the separator used in the file
		COLUMN_TYPES : a list specifying the types for each of the columns in the file
			the special type NONE specifies that the column is not used
			Each element in the list is a tuple (column_name, type)

		MAX_LINE_SIZE : is the maximum size of a line. If not defined, it is set to 10000
		TUPLES_PER_CHUNK : the number of tuples in each chunk
		PAGE_SIZE_4K_MULT : the size of the page as a 4K multiplier

		These definitions are expected in the file FileLoaderDefs.m4

		The same definitions are used in FileLoader.cc.m4
*/

dnl # M4 CODE
include(FileLoaderFunctions.m4)
m4_include(FileLoaderDefs.m4)
dnl # END OF M4 CODE

#include "EventProcessor.h"
#include "Message.h"
#include "DataTypes.h"
#include "DistributedCounter.h"


/** class to populate columns with values and send them off to a file writer */
class FileLoaderImp : public EventProcessorImp {
private:
	off_t chunksProduced; // how many chunks we produced so far
	off_t tuplesHandled; // how many tuples we have handled so far
	DistributedCounter* dCounter; // distributed counter to detect when all the loaders are done
	off_t pageSize; // the size of the page (to know how to allocate the memory with mmap)

	dnl # M4 CODE
	dnl # declaring all the columns
	m4_foreach(</_C_/>, </COLUMN_TYPES/>, </dnl
		M4_DECLARE_COLUMN(_C_)dnl
	/>)
	dnl # END OF M4 CODE

	EventProcessor fileWriter; // the file writer that can write the chunks
	char* fileName; // the name of the file that we need to scan
	FILE* stream; // the file descriptor
	bool finished; // turns to true when the whole file is read

	// automate the filling of chunks
	void FillAndSendChunk(void);
	// stop function
	void Done(void);

public:
	// the arguments to the constructor are the file containing the data
	// and the file writer that can write the data
	FileLoaderImp(const char* _fileName, EventProcessor& _fileWriter, DistributedCounter* _dCounter);

	// destructor
	virtual ~FileLoaderImp(void);

	// Message handler for the return OK
	MESSAGE_HANDLER_DECLARATION(ChunkWrittenProc);
};


#endif // _FILELOADERIMP_H_
